﻿namespace Swimj.Core.Utilities
{
    public static class HttpResponseMessageHelper
    {
        public static string GetFileName(HttpResponseMessage response)
        {
            string fileName = string.Empty;

            if (response.Content.Headers.ContentDisposition!=null)
            {
                var contentDisposition = response.Content.Headers.ContentDisposition?.ToString();
                if (!string.IsNullOrEmpty(contentDisposition))
                {
                    var fileNamePart = contentDisposition
                        .Split(';')
                        .FirstOrDefault(x => x.Trim().StartsWith("filename="));
                    if (!string.IsNullOrEmpty(fileNamePart))
                    {
                        fileName = fileNamePart.Split('=')[1].Trim('"');
                    }
                }

            }
            return fileName;
        }
    }
}
